/**
 * Created by lt on 2017/5/4.
 */
/*活动-编辑*/
function member_edit(title,url,id,w,h){
    layer_show(title,url,w,h);
}

/*活动-编辑*/
function picture_edit(title,url,id){
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

/*弹出小框*/
function productTypeEdit(title,url,w,h){
    layer_show(title,url,w,h);
}


/*根据产品名称查询产品*/
function setActivityProduct(activityId) {
    var divhtml = '<div id="sellerList">';
    divhtml += '<div class="row cl">';
    divhtml += ' <div class="formControls col-xs-8 col-sm-9">';
    divhtml += '<span class="select-box">';
    divhtml += '  <input type="hidden"  id="activityId">';
    divhtml += '  <input type="hidden"  id="productEntityId">';
    divhtml += '  <input type="text" class="input-text"  placeholder="输入产品类型名称" id="productEntityName1">';
    divhtml += '</span>';
    divhtml += '</div>';
    divhtml += '</div> <div class="row cl">';
    divhtml += '<div class="formControls col-xs-8 col-sm-9">';
    divhtml += '<table id="tab" class="table table-border table-bordered table-bg table-hover table-sort">';
    divhtml += '<thead>';
    divhtml += '<tr class="text-c">';
    divhtml += ' <th width="12%">产品类型编号</th>';
    divhtml += ' <th width="12%">产品类型名称</th>';
    divhtml += '<th width="12%">产品类型介绍</th>';
    divhtml += '        </tr>';
    divhtml += '</thead>';
    divhtml += '<tbody id="orgList">';
    divhtml += '</tbody>';
    divhtml += ' </table>';
    divhtml += '</div>';
    divhtml += '</div>';
    divhtml += '  <div class="row cl">';
    divhtml += '<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">';
    divhtml += '<input class="btn btn-primary radius" type="submit" id="sellerNameSearch" value="&nbsp;&nbsp;搜索&nbsp;&nbsp;">';
    divhtml += '&nbsp;&nbsp;&nbsp;&nbsp;';
    divhtml += '<input class="btn btn-primary radius" type="submit" id="sellerNameSubmit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">';
    divhtml += '</div>';
    divhtml += '</div>';
    divhtml += '</div>';

    layerTools.layerShowDIv(divhtml, '');
    $("#activityId").val(activityId);
    var postUrl = ctx + '/admin/product/getProductEntityByName.html?activityId='+activityId;
    //var postData = {"productEntityName": $("#productEntityName1").val()};
    ajaxRequestMethod("sellerNameSearch",postUrl,1);
}



function getProductData(productEntityId, productName) {
   //选中颜色变化
    var trs = $("#orgList").children("tr")
    for( var i=0; i<trs.length; i++ ){
        trs[i].onmousedown = function(){
            tronmousedown(this);
        }
    }
    //参数设置
    $("#productEntityId").val(productEntityId);
    var postUrl = ctx + '/admin/activity/setActivityProduct.html';
    ajaxRequestMethod("sellerNameSubmit",postUrl,2);

}

function tronmousedown(obj){
    var trs = $("#orgList").children("tr")
    for( var o=0; o<trs.length; o++ ){
        if( trs[o] == obj ){
            trs[o].style.backgroundColor = '#DFEBF2';
        }
        else{
            trs[o].style.backgroundColor = '';
        }
    }
}



function isemptyStr(str) {
    return (str == undefined ? "" : str);
}



function ajaxRequestMethod(fieldName,postUrl,status){
    $("#"+fieldName).unbind();
    $("#"+fieldName).bind("click", function () {
       /* var postUrl = ctx + '/admin/product/getProductEntityByName.html';*/
       if(status==1){
           var postData = {"productEntityName": $("#productEntityName1").val()};
       }
       if(status==2){
           var postData = {"activityId": $("#activityId").val(),"productEntityId": $("#productEntityId").val()};
       }
        $.ajax({
            type: 'POST',
            url: postUrl,
            dataType: 'json',
            data: postData,
            success: function (data) {
                console.log(data);
                if (data.code != 1) {
                    layer.msg(data.msg, {icon: 1, time: 1000});
                    return;
                }
                if(status==1){
                    var sellerList = data.data;
                    var html = "";
                    if(sellerList==0){
                        html +="<tr><td colspan ='2'><center>没有匹配数据！</center></td></tr>";
                    }
                    for (i = 0; i < sellerList.length; i++) {
                        html += "  <tr onclick='getProductData(\"" + sellerList[i].productEntityId + "\",\"" + sellerList[i].productEntityName + "\")'><td >" + sellerList[i].productEntityId + "</td><td >" + sellerList[i].productEntityName + "</td><td>" + isemptyStr(sellerList[i].productEntityIntroduction) + "</td></tr>";
                    }
                    $("#orgList").html(html);
                }
               if(status==2){
                   if (data.code != 1){
                       layer.msg(data.msg,{icon : 5,time : 2000});
                       console.log(data.msg);
                       return;
                   }
                   layer.msg('操作成功!',{icon : 6,time : 1000})
                   reload();
               }
            },
            error: function (data) {
                console.log(data.msg);
            }
        })
    });
}

//页面刷新
function reload(){
    var postUrl = ctx + '/admin/product/getProductEntityByName.html?activityId='+$("#activityId").val();
    ajaxRequest(postUrl,0)
}


/*活动-二维码*/
function twoDimensionCode(activityId,activityName,activityUrl){
    layer.confirm('确认生成吗？',function(index){
        var postData = {"activityId": activityId,"activityName": activityName,"activityUrl": activityUrl};
        $.ajax({
            cache:true,
            type:'POST',
            data: postData,
            url:'/admin/activity/createActivityQRCode.html',
            dataType: 'json',
            async: false,
            success:function(data){
                console.log(data);
               // alert(data.code);
                if (data.code != 1){
                    layer.msg('操作失败!',{icon : 5,time : 1000});
                    console.log(data.msg);
                    return;
                }
                layer.msg('操作成功!',{icon : 6,time : 1000},function(){
                      location.reload();
                    // var index = parent.layer.getFrameIndex(window.name);
                    // parent.layer.close(index);
                })
            }
        })
    });
}

//活动已关联产品展示
function showActivityProductEntity(activityId) {
    var divhtml = '<div id="sellerList">';
    divhtml += '<div class="row cl">';
    divhtml += ' <div class="formControls col-xs-8 col-sm-9">';
    divhtml += '<span class="select-box">';
    divhtml += '  <input type="hidden"  id="activityId">';
    divhtml += '  <input type="hidden"  id="productEntityId">';
    divhtml += '  <h4 style="text-align:center;">活动已关联产品</h4>';
    divhtml += '</span>';
    divhtml += '</div>';
    divhtml += '</div> <div class="row cl">';
    divhtml += '<div class="formControls col-xs-8 col-sm-9">';
    divhtml += '<table id="tab" class="table table-border table-bordered table-bg table-hover table-sort">';
    divhtml += '<thead>';
    divhtml += '<tr class="text-c">';
    divhtml += ' <th width="12%">产品类型编号</th>';
    divhtml += ' <th width="12%">产品类型名称</th>';
    divhtml += '<th width="12%">产品类型介绍</th>';
    divhtml += '        </tr>';
    divhtml += '</thead>';
    divhtml += '<tbody id="proList">';
    divhtml += '</tbody>';
    divhtml += ' </table>';
    divhtml += '</div>';
    divhtml += '</div>';
    divhtml += '</div>';

    layerTools.layerShowDIv(divhtml, '');
    var postUrl = ctx + '/admin/activity/getProductEntityByActivityId.html?activityId='+activityId;
    ajaxRequest(postUrl,1);
}

function ajaxRequest(postUrl,status){
    $.ajax({
        type: 'POST',
        url: postUrl,
        dataType: 'json',
        data: null,
        success: function (data) {
            console.log(data);
            if (data.code != 1) {
                layer.msg(data.msg, {icon: 1, time: 1000});
                return;
            }
            var sellerList = data.data;
            var html = "";
            if(sellerList==0){
                html +="<tr><td colspan ='2'><center>没有匹配数据！</center></td></tr>";
            }
            for (i = 0; i < sellerList.length; i++) {
                html += "  <tr onclick='getProductData(\"" + sellerList[i].productEntityId + "\",\"" + sellerList[i].productEntityName + "\")'><td >" + sellerList[i].productEntityId + "</td><td >" + sellerList[i].productEntityName + "</td><td>" + isemptyStr(sellerList[i].productEntityIntroduction) + "</td></tr>";
            }
            if(status == 0){
                $("#orgList").html(html);
            }else{
                $("#proList").html(html);
            }
        },
        error: function (data) {
            console.log(data.msg);
        }
    })
}